package com.qianfeng.zhangwei.sybl.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.TypeReference;
import com.gc.flashview.FlashView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.RequestParams;
import com.qianfeng.zhangwei.sybl.R;
import com.qianfeng.zhangwei.sybl.adapter.ActivityAdapter;
import com.qianfeng.zhangwei.sybl.bean.Activities;
import com.qianfeng.zhangwei.sybl.bean.Banner;
import com.qianfeng.zhangwei.sybl.bean.Entity;
import com.qianfeng.zhangwei.sybl.config.UrlContants;
import com.qianfeng.zhangwei.sybl.utils.AsyncHttpUtils;

import java.util.ArrayList;


public class ActivityFragment extends BaseFragment implements PullToRefreshBase.OnRefreshListener2<ListView> {
    private PullToRefreshListView ptrListView;
    private ActivityAdapter adapter;
    private ArrayList<Activities> list = new ArrayList<>();
    private int page;
    private FlashView flashView;
    private ArrayList<Banner> banners = new ArrayList<>();
    private View headView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_activity, container, false);
        createHeadView(inflater);
        initView(view);
        initBanner();
        return view;
    }

    private void initView(View view) {
        ptrListView = (PullToRefreshListView) view.findViewById(R.id.fragment_base_ptrlistview);
        ptrListView.setOnRefreshListener(this);
        ptrListView.getRefreshableView().addHeaderView(headView);
        adapter = new ActivityAdapter(list, activity);
        ptrListView.getRefreshableView().setAdapter(adapter);
        ptrListView.setRefreshing();
    }

    private void createHeadView(LayoutInflater inflater) {
        headView = inflater.inflate(R.layout.widget_activity_head_layout, null);
        flashView = (FlashView) headView.findViewById(R.id.activ_head_flash_view);
    }


    private void initData() {
        TypeReference<Entity<Activities>> type = new TypeReference<Entity<Activities>>() {
        };
        AsyncHttpUtils.getInstance().post(UrlContants.URL_ACTIVITY, type, getReqeustParams(), ptrListView, list, adapter, page);
    }

    private void initBanner() {
        RequestParams params = new RequestParams();
        params.put("platform", "2");
        params.put("pos", "1");
        params.put("compare", "54f3d5b6cd0b84fa56e3aff0de20068f");
        AsyncHttpUtils.getInstance().postBannerData(params, flashView, banners);
    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        page = 1;
        if (refreshView.getMode() != PullToRefreshBase.Mode.BOTH) {
            refreshView.setMode(PullToRefreshBase.Mode.BOTH);
        }
        initData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        page++;
        initData();
    }

    private RequestParams getReqeustParams() {
        RequestParams params = new RequestParams();
        params.add("platform", "2");
        params.add("compare", "d0e2e22365013c957cdb5fe58e2c6617");
        return params;
    }

}
